$(document).ready(function(){
    var check_code = false;
    var countdown = 60;
    $('#getcode').on('click',function(){
        var mobile = $('#mobile').val();
        if(mobile == ""){
            layer.open({
                content: '请输入手机号！',
                skin: 'msg',
                time: 3 //3秒后自动关闭
            });
            return false;
        }
        
        if(mobileCheck(mobile) === false){
            return false;
        }
        
        $.ajax({
            //请求方式
            type : "GET",
            //请求的媒体类型
            contentType: "application/json;charset=UTF-8",
            //请求地址
            url : "/api/user/getmobilecode",
            async:false,
            data : {'mobile':mobile},
            //请求成功
            success : function(result) {
                console.log(result);
                $('#hashcode').val(result.hashcode)
                settime();
            },
            //请求失败，包含具体的错误信息
            error : function(e){
                console.log(e.status);
                console.log(e.responseText);
            }
        });
    });
    
    function settime() { //发送验证码倒计时
        if (countdown == 0) { 
            $('#getcode').attr('disabled',false); 
            //obj.removeattr("disabled"); 
            $('#getcode').val("获取验证码");
            countdown = 60; 
            return;
        } else { 
            $('#getcode').attr('disabled',true);
            $('#getcode').val("重新发送(" + countdown + ")");
            countdown--; 
        }
        setTimeout(function() {
            settime() 
        },1000)
    }
    
    function mobileCheck(mobile){
        var check_result = true;
        
        if(mobile === ""){
            layer.open({
                content: '请输入手机号！',
                skin: 'msg',
                time: 3 //3秒后自动关闭
            });
            return false;
        }
        return check_result;
    }
    
    function codeCheck(code,hashcode){
        var check_result = false;
        
        $.ajax({
            //请求方式
            type : "GET",
            //请求的媒体类型
            contentType: "application/json;charset=UTF-8",
            //请求地址
            url : "/api/default/checkcode",
            async:false,
            data : {code:code,hashcode:hashcode},
            //请求成功
            success : function(result) {
//                console.log(result);
                if(result.success==0){
                    layer.open({
                        content: result.msg,
                        skin: 'msg',
                        time: 3 //3秒后自动关闭
                    });
                    return false;
                }else{
                    check_result = true;
                }
            },
            //请求失败，包含具体的错误信息
            error : function(e){
                console.log(e.status);
                console.log(e.responseText);
            }
        });
        return check_result;
    }
    
    function passwdCheck(passwd){
        if(passwd === ""){
            layer.open({
                content: '请输入密码！',
                skin: 'msg',
                time: 3 //3秒后自动关闭
            });
            return false;
        }
        return true;
    }
    
    function rePasswdCheck(passwd,re_passwd){
        if(passwd !== re_passwd){
            layer.open({
                content: '请确认密码！',
                skin: 'msg',
                time: 3 //3秒后自动关闭
            });
            return false;
        }
        return true;
    }
    
    $('#mobilebind-submit').on('click',function(){
        
        var code = $("#code").val();
        var hashcode = $("#hashcode").val();
        if(hashcode === ""){
            layer.open({
                content: '请先获取验证码！',
                skin: 'msg',
                time: 3 //3秒后自动关闭
            });
            return false;
        }
        
        var code_check = codeCheck(code,hashcode);
        
        var passwd = $('#passwd').val();
        var re_passwd = $('#re_passwd').val();
        
        if(code_check === true && passwdCheck(passwd) && rePasswdCheck(passwd,re_passwd)){
            $('#mobilebind-form').submit();
        }
        
    });
});
